package cn.dcw.pethome.org.service.impl;

import cn.dcw.pethome.basic.service.IBaseService;
import cn.dcw.pethome.basic.service.impl.BaseServiceImpl;
import cn.dcw.pethome.basic.util.PageList;
import cn.dcw.pethome.org.domain.Department;
import cn.dcw.pethome.org.domain.Employee;
import cn.dcw.pethome.org.mapper.DepartmentMapper;
import cn.dcw.pethome.org.mapper.EmployeeMapper;
import cn.dcw.pethome.org.query.DepartmentQuery;
import cn.dcw.pethome.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
/*
部门管理
 */
@Service

public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {
    //加载部门mapper
    @Autowired
    private DepartmentMapper mapper;

    //添加部门
    @Override
    @Transactional
    public void add(Department department) {
        mapper.add(department);
       this.update(department);
    }


    @Override
    @Transactional
    public void update(Department department) {
        //顶级部门，上级id为null,dirPath就是自己
        if(department.getDepartment()==null){
            //department.getId()数据库中获取的自增长id
            department.setDirPath("/"+department.getId());
        }else {//有上级部门，查到上级部门的parent_id，获取到dirPath，进行拼接
            Long id = department.getDepartment().getId();
            if(id!=null){

                String dirPath = mapper.findOne(id).getDirPath();
                department.setDirPath(dirPath+"/"+department.getId());//设置新的dirPath
            }
        }
        mapper.update(department);
    }

    /**
     * 二级联动
     * @return
     */
    @Override
    public List<Department> getTree() {
        return mapper.getTree();
    }
}
